/**
 *
 */
package ua.at.tsvetkov.samizdat.adapters;

import ua.at.tsvetkov.samizdat.Const;
import ua.at.tsvetkov.samizdat.R;
import ua.at.tsvetkov.samizdat.data.DataDownloader;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @author A.Tsvetkov 2011, http://tsvetkov.at.ua, al@ukr.net Адаптер основного меню.
 */
public abstract class DataReaderAdapter extends BaseAdapter {

	protected Activity			context;
	protected DataDownloader	dataLoader;
	protected LayoutInflater	mInflater;
	protected int					partSize;
	private int						size;
	private int						currentStart;
	private int						currentEnd;

	public DataReaderAdapter(Activity context, DataDownloader dataLoader, int partSize) {
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		this.dataLoader = dataLoader;
		this.partSize = partSize;
		this.size = dataLoader.size();
		this.currentStart = 0;
		this.currentEnd = 0;
	}

	/**
	 * @param listView
	 * @param currentStart the currentStart to set
	 */
	public void update(final ListView listView) {
		this.size = dataLoader.size();
		this.currentStart = dataLoader.currentStart();
		this.currentEnd = currentStart + dataLoader.currentSize();
		context.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				DataReaderAdapter.this.notifyDataSetChanged();
				listView.setSelection(0);
			}
		});

	}

	/*
	 * (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		return dataLoader.currentSize() + 2;
	}

	/*
	 * (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int index) {
		if (index == 0 || index == dataLoader.currentSize() + 1)
			return "";
		return dataLoader.getData(index + 1);
	}

	/*
	 * (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (position == 0 || position == dataLoader.currentSize() + 1) {
			MoreViewHolder holder = new MoreViewHolder();
			convertView = mInflater.inflate(R.layout.adt_list_more, null);
			holder.text = (TextView) convertView.findViewById(R.id.AdtListMoreMore);
			holder.count = (TextView) convertView.findViewById(R.id.AdtListMoreCount);
			convertView.setTag(holder);
			if (position == 0) {
				if (currentStart == 0) {
					convertView = mInflater.inflate(R.layout.empty, null);
				} else {
					holder.text.setText(context.getString(R.string.PREVIOUS));
					holder.count.setTextColor(R.color.Black);
					holder.count.setText(Const.LIST_VIEW_COUNT + " " + context.getString(R.string.FROM) + " " + currentStart);
				}
			} else {
				if (currentEnd == size || size - currentEnd < 0) {
					convertView = mInflater.inflate(R.layout.empty, null);
				} else {
					holder.text.setText(context.getString(R.string.NEXT));
					holder.count.setTextColor(R.color.Black);
					holder.count.setText(Const.LIST_VIEW_COUNT + " " + context.getString(R.string.FROM) + " " + (size - currentEnd));
				}
			}
			return convertView;
		} else {
			return null;
		}
	}

	public abstract void refreshData();
}

class MoreViewHolder {
	TextView	text;
	TextView	count;
}
